home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gekikoh Dennoh Club 5
/
Gekikoh Dennoh Club Vol. 5 (Japan).7z
/
Gekikoh Dennoh Club Vol. 5 (Japan) (Track 01).bin
/
internet
/
tcppack
/
tcppackb.lzh
/
src
/
samples
/
netstat.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-26
|
11KB
|
480 lines
/*
* netstat.c
*
* Copyright (C) 1994 Tomoaki Tada/F.C.T.
*/
static char *rcsid = "$Id: netstat.c,v 1.5 1994/08/04 05:02:08 Niggle Exp $";
/*
* $Log: netstat.c,v $
* Revision 1.5 1994/08/04 05:02:08 Niggle
* inetdé¬ÅφÆôé╡é─éóé╚éóÅΩìçé╠ò\Īé≡ò╧ìX
*
* Revision 1.4 1994/08/03 01:58:42 Niggle
* alias é≡Ägùpé╖éΘé╠é≡éΓé▀é─üCnameé⌐éτdomain nameé≡Å£éóé╜éαé╠é≡Ägéñéµéñò╧ìX
*
* Revision 1.3 1994/07/19 12:24:35 Niggle
* default routeé╠ò\Īé≡é╖éΘéµéñé╔ò╧ìX
* get{host,net}by{addr,name}()é╔æ╬ë₧
* òsùvé╚âIâvâVâçâôé≡getopt()é⌐éτô╛é╚éóéµéñé╔ò╧ìX
*
* Revision 1.2 1994/06/23 04:02:35 Niggle
* sigintè╓ÿAé╠ò╧ìX
*
* Revision 1.1 1994/06/22 02:00:15 Niggle
* Initial revision
*
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<unistd.h>
#include<signal.h>
#ifdef __human68k__
#undef __DOS_INLINE__
#define __DOS_INLINE__
#include<sys/dos.h>
#endif
#include"network.h"
#include"socket.h"
static int num_of_socket;
static long *sockets;
static int no_convert_p; /* Don't convert ip to hostname */
static int statics_p; /* Show statics */
static int sleep_time;
static void display_interface_list (char *);
static void display_interface_list_c (char *);
static void display_unix_socket (void);
static void display_inet_socket (void);
static void display_route_table (void);
static void display_a_route (route *);
static char * itoa_rflag (route *);
static char *ipaddr2host (long);
static char *ipaddr2net (long);
/************************************************
* *
************************************************/
void
main (int argc, char **argv)
{
long version = _get_version ();
int unix_p, inet_p, route_p, iface_list_p;
char *iface_name;;
unix_p = inet_p = route_p = iface_list_p = 0;
no_convert_p = statics_p = 0;
iface_name = NULL;
if (version == -1)
fprintf (stderr, "TCP/IP driveré═ÅφÆôé╡é─éóé▄é╣é±\n");
else
{
int c;
opterr = 1;
while ((c = getopt (argc, argv, "f:I:inp:rsw:")) != EOF)
{
switch (c)
{
case 'f':
if (!strcmp (optarg, "inet"))
inet_p = 1;
else if (!strcmp (optarg, "local"))
unix_p = 1;
else
printf ("unknown address type.\n");
break;
case 'I':
iface_name = optarg;
iface_list_p = 1;
break;
case 'i':
/* display all table */
iface_list_p = 1;
break;
case 'n':
no_convert_p = 1;
break;
case 'p':
/* TODO */
break;
case 'r':
route_p = 1;
break;
case 's':
statics_p = 1;
break;
case 'w':
sleep_time = atoi (optarg);
iface_list_p = 1;
break;
default:
break;
}
}
num_of_socket = 0;
sockets = get_sock_array (&num_of_socket);
if (sockets)
{
if (!(inet_p || unix_p || iface_list_p || route_p))
{
display_inet_socket ();
return;
}
if (inet_p)
display_inet_socket ();
if (unix_p)
display_unix_socket ();
free (sockets);
}
if (sleep_time)
display_interface_list_c (iface_name);
else if (iface_list_p)
display_interface_list (iface_name);
if (route_p)
display_route_table ();
}
return;
}
/************************************************
* *
************************************************/
static void
display_interface_list (char *i_name)
{
iface *top, *cur;
top = get_iface_list ();
if (top)
{
printf ("Name Mtu NetWork Address Ipkts Ierrs Opkts Oerrs Coll\n");
for (cur = top; cur != NULL; cur = cur->next)
{
char buff[32];
char buff2[32];
char *net, *name;
if (i_name && strcmp (cur->name, i_name))
continue;
cur->update (cur);
name = ipaddr2host (cur->my_ip_addr);
net = ipaddr2net (cur->my_ip_addr & cur->net_mask);
n2a_ipaddr (cur->my_ip_addr, buff);
n2a_ipaddr (cur->my_ip_addr & cur->net_mask, buff2);
printf ("%4s %4.4d %-10s %-10s %8d %4d %8d %4d %4d\n",
cur->name,
cur->mtu,
net && ! no_convert_p ? net : buff2,
name && ! no_convert_p ? name : buff,
cur->rawrcvcnt /*+ cur->iprcvcnt*/,
cur->rcverrcnt,
cur->rawsndcnt /*+ cur->ipsndcnt*/,
cur->snderrcnt,
cur->collcnt);
}
fflush (stdout);
}
return;
}
/************************************************
* *
************************************************/
static void
calc_iface_stat (iface *top, iface *src, long *total, long *diff, long *total2, long *diff2)
{
iface *cur;
long _total[5];
bzero (_total, sizeof (long) * 5);
for (cur = top; cur != NULL; cur = cur->next)
{
cur->update (cur);
if (cur == src)
{
diff2[0] = cur->rawrcvcnt - total2[0];
diff2[1] = cur->rcverrcnt - total2[1];
diff2[2] = cur->rawsndcnt - total2[2];
diff2[3] = cur->snderrcnt - total2[3];
diff2[4] = cur->collcnt - total2[4];
total2[0] = cur->rawrcvcnt;
total2[1] = cur->rcverrcnt;
total2[2] = cur->rawsndcnt;
total2[3] = cur->snderrcnt;
total2[4] = cur->collcnt;
}
_total[0] += cur->rawrcvcnt;
_total[1] += cur->rcverrcnt;
_total[2] += cur->rawsndcnt;
_total[3] += cur->snderrcnt;
_total[4] += cur->collcnt;
}
diff[0] = _total[0] - total[0];
diff[1] = _total[1] - total[1];
diff[2] = _total[2] - total[2];
diff[3] = _total[3] - total[3];
diff[4] = _total[4] - total[4];
total[0] = _total[0];
total[1] = _total[1];
total[2] = _total[2];
total[3] = _total[3];
total[4] = _total[4];
return;
}
/************************************************
* *
************************************************/
static void
sigint_handler (int dummy)
{
return;
}
/************************************************
* *
************************************************/
static void
display_interface_list_c (char *i_name)
{
iface *top, *cur;
long total[5], diff[5], Itotal[5], Idiff[5];
top = get_iface_list ();
if (top)
{
for (cur = top; cur != NULL; cur = cur->next)
{
if (!i_name)
break;
if (!strcmp (cur->name, i_name))
break;
}
if (cur)
{
signal (SIGINT, sigint_handler);
for (;;)
{
int ii;
bzero (total, sizeof (long) * 5); bzero (diff, sizeof (long) * 5);
bzero (Itotal, sizeof (long) * 5); bzero (Idiff, sizeof (long) * 5);
printf (" input (%3s) output input (Total) output\n",
cur->name);
printf (" packets errs packets errs colls packets errs packets errs colls\n");
for (ii = 0; ii < 21; ii++)
{
/* make total */
calc_iface_stat (top, cur, total, diff, Itotal, Idiff);
/* display diff */
printf ("%8d %4d %8d %4d %4d %8d %4d %8d %4d %4d\n",
Idiff[0], Idiff[1], Idiff[2], Idiff[3], Idiff[4],
diff[0], diff[1], diff[2], diff[3], diff[4]);
fflush (stdout);
#ifdef __human68k__
while (_dos_keysns ())
_dos_getchar ();
#endif
sleep (sleep_time);
}
}
}
}
return;
}
/************************************************
* *
************************************************/
static void
display_unix_socket (void)
{
printf ("Sorry, not supported.\n");
}
/************************************************
* *
************************************************/
static void
display_inet_socket (void)
{
int i, exist;
exist = 0;
for (i = 0; i < num_of_socket; i++)
{
char buff[256];
if (ntoa_sock (sockets[i], AF_INET, buff, no_convert_p))
{
if (!exist)
{
printf ("Active Internet Connections\n");
printf ("Proto Recv-Q Send-Q Local Address Foreign Address (state)\n");
}
exist = 1;
printf ("%s\n", buff);
}
}
}
/************************************************
* *
************************************************/
static char *
itoa_rflag (route *cur)
{
static char buff[8];
char *p;
iface *ifp;
buff[0] = 'U';
p = &buff[1];
ifp = cur->iface;
if (cur->target && (cur->target & ~(ifp->net_mask)))
*p++ = 'H';
if (cur->gateway)
*p++ = 'G';
*p++ = '\0';
return buff;
}
/************************************************
* *
************************************************/
static void
display_route_table (void)
{
route *cur;
route *def;
rtable *Routes;
int x, y;
Routes = (rtable *)rt_top (&def);
if (!Routes)
return;
printf ("Routing tables\n");
printf ("Destination Gateway Flags Use Interface\n");
if (def && def->iface)
display_a_route (def);
for (x = 0; x < 32; x++)
{
for (y = 0; y < NROUTE; y++)
{
for (cur = (*Routes)[x][y]; cur != NULL; cur = cur->next)
display_a_route (cur);
}
}
fflush (stdout);
return;
}
/************************************************
* *
************************************************/
static void
display_a_route (route *cur)
{
char target[32], gateway[32], gateway1[32];
char *net, *gateway2;
if (no_convert_p)
{
net = NULL;
if (cur->gateway)
gateway2 = gateway;
else
{
n2a_ipaddr (cur->iface->my_ip_addr, gateway1);
gateway2 = gateway1;
}
}
else
{
net = ipaddr2net (cur->target);
if (!net)
net = ipaddr2host (cur->target);
if (cur->gateway)
gateway2 = ipaddr2host (cur->gateway);
else
gateway2 = ipaddr2host (cur->iface->my_ip_addr);
}
if (cur->target)
n2a_ipaddr (cur->target, target);
else
strcpy (target, "default");
n2a_ipaddr (cur->gateway, gateway);
printf ("%-16s %-16s %-5s %5d %9s\n",
net ? : target, gateway2 ? : gateway,
itoa_rflag (cur),
cur->uses, cur->iface->name);
}
/************************************************
* *
************************************************/
static char *
ipaddr2host (long ipaddr)
{
struct hostent *h;
h = gethostbyaddr ((char *)&ipaddr, sizeof (long), AF_INET);
if (h)
{
char *p, *q;
p = malloc (strlen (h->h_name) + 1);
if (!p)
return NULL;
strcpy (p, h->h_name);
q = strchr (p, '.');
if (q)
{
if (!strcmp (q + 1, get_domain_name ()))
*q = 0;
}
return p;
}
return NULL;
}
/************************************************
* *
************************************************/
static char *
ipaddr2net (long ipaddr)
{
struct netent *h;
h = getnetbyaddr (ipaddr, AF_INET);
if (h)
return h->n_name;
return NULL;
}